//
// Created by xili on 2024/9/1 23:23.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    int scheduleCourse(vector <vector<int>> &courses) {
        sort(courses.begin(), courses.end(), [](const auto &a, const auto &b) { return a[1] < b[1]; });
        priority_queue<int> heap;
        int time = 0;
        for (auto &it: courses) {
            if (time + it[0] <= it[1]) {
                heap.push(it[0]);
                time += it[0];
            } else if (!heap.empty() && heap.top() > it[0]) {
                time += it[0] - heap.top();
                heap.pop();
                heap.push(it[0]);
            }
        }
        return heap.size();
    }
};